import { Toast } from "vant"

export default {
  namespaced: true,
  state: {
    user: JSON.parse(localStorage.user || '[]')
  },
  mutations: {
    REGISTER(state, payload) {
      if (state.user.length === 0) {
        state.user.push(payload)
        Toast('注册成功!')
        localStorage.user = JSON.stringify(state.user)
        setTimeout(() => {
          location.href = '/login'
        }, 1000)
        return
      }
      const res = state.user.every(item => {
        return item.username !== payload.username
      })
      console.log(res)
      if (res) {
        Toast('注册成功!')
        state.user.push(payload)
        localStorage.user = JSON.stringify(state.user)
        setTimeout(() => {
          location.href = '/login'
        }, 1000)
      } else {
        Toast('该用户已注册!')
      }
    }
  },
  actions: {

  }
}